<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>webpack学习：进阶知识点 - smartzheng</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="smartzheng" /><meta name="description" content="1.Tree Shaking 移除JavaScript上下文中的未引用代码(dead-code)，需要将mode选项设置为production开启，且只支持ES Module 配置" /><meta name="keywords" content="Hugo, theme, even" />






<meta name="generator" content="Hugo 0.68.3 with theme even" />


<link rel="canonical" href="https://smartzheng.github.io/post/webpack%E5%AD%A6%E4%B9%A0%E4%B9%8B%E8%BF%9B%E9%98%B6%E7%9F%A5%E8%AF%86%E7%82%B9/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">


<link href="/dist/even.c2a46f00.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="webpack学习：进阶知识点" />
<meta property="og:description" content="1.Tree Shaking 移除JavaScript上下文中的未引用代码(dead-code)，需要将mode选项设置为production开启，且只支持ES Module 配置" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://smartzheng.github.io/post/webpack%E5%AD%A6%E4%B9%A0%E4%B9%8B%E8%BF%9B%E9%98%B6%E7%9F%A5%E8%AF%86%E7%82%B9/" />
<meta property="article:published_time" content="2019-08-08T00:00:00+00:00" />
<meta property="article:modified_time" content="2019-08-08T00:00:00+00:00" />
<meta itemprop="name" content="webpack学习：进阶知识点">
<meta itemprop="description" content="1.Tree Shaking 移除JavaScript上下文中的未引用代码(dead-code)，需要将mode选项设置为production开启，且只支持ES Module 配置">
<meta itemprop="datePublished" content="2019-08-08T00:00:00&#43;00:00" />
<meta itemprop="dateModified" content="2019-08-08T00:00:00&#43;00:00" />
<meta itemprop="wordCount" content="1256">



<meta itemprop="keywords" content="webpack," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="webpack学习：进阶知识点"/>
<meta name="twitter:description" content="1.Tree Shaking 移除JavaScript上下文中的未引用代码(dead-code)，需要将mode选项设置为production开启，且只支持ES Module 配置"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">smartzheng</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">Home</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">Archives</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">Tags</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">Categories</li>
      </a>
  </ul>
</nav>
  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">smartzheng</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">Home</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">Archives</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">Tags</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">Categories</a>
      </li>
  </ul>
</nav>
    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">webpack学习：进阶知识点</h1>

      <div class="post-meta">
        <span class="post-time"> 2019-08-08 </span>
        <div class="post-category">
            <a href="/categories/web%E5%89%8D%E7%AB%AF/"> web前端 </a>
            </div>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#1tree-shaking">1.Tree Shaking</a></li>
        <li><a href="#2codesplitting">2.codeSplitting</a></li>
        <li><a href="#3打包分析常用工具">3.打包分析常用工具</a></li>
        <li><a href="#4webpack与浏览器缓存">4.webpack与浏览器缓存</a></li>
        <li><a href="#5shimming">5.Shimming</a></li>
        <li><a href="#6library基本配置">6.Library基本配置</a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h3 id="1tree-shaking">1.Tree Shaking</h3>
<p>移除JavaScript上下文中的未引用代码(dead-code)，需要将mode选项设置为production开启，且只支持ES Module<br>
配置：</p>
<ul>
<li>package.json-&gt;useExports:true/false true为开启</li>
<li>sideEffects:false/[]，false为对所有文件都开启Tree Shaking，但在导入时会执行特殊行为的代码，而不是仅仅暴露一个export或多个export。例如<code>import '@bable/poly-fill'</code>，它影响全局作用域，并且通常不提供export。这时需要手动加到sideEffects数组中防止被删除。</li>
</ul>
<h3 id="2codesplitting">2.codeSplitting</h3>
<h4 id="js代码分割">js代码分割</h4>
<p>概念：代码分割，与webpack无关，但webpack可以帮助开发者自动进行代码分割<br>
两种方式：<br>
1.optimization配置<br>
2.异步import会自动被代码分割<br>
webpack中结合SplitChunksPlugin实现代码分割</p>
<p><strong>为什么webpack中splitChunks默认只针对async引入的代码？</strong><br>
js优化两个方向：缓存和代码使用率<br>
缓存: splitChunks:&lsquo;all&rsquo; 相同chunk代码不重复加载；提升较小，因为只能提高第二次访问时的速度<br>
代码使用（覆盖）率（coverage）: preLoading（初始页面同时预加载）、preFetching（网络空闲预加载） 提升较大</p>
<h4 id="css代码优化">CSS代码优化</h4>
<p>分割：MiniCssExtractPlugin(不支持HMR，适合使用在production)<br>
压缩：OptimizeCSSAssetsPlugin</p>
<h3 id="3打包分析常用工具">3.打包分析常用工具</h3>
<p>webpack/analyse，bundle analysis</p>
<h3 id="4webpack与浏览器缓存">4.webpack与浏览器缓存</h3>
<p>由于浏览器缓存的原因，若每次打包生成的文件名都一样，那有可能新代码上线后浏览器还是访问的本地缓存，可以output配置name和chunkFilename时加上contenthash标识：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">output: {
	filename: &#39;[name].[contenthash].js&#39;,
	chunkFilename: &#39;[name].[contenthash].js&#39;
}
</code></pre></td></tr></table>
</div>
</div><h3 id="5shimming">5.Shimming</h3>
<p>更改部分代码的默认行为<br>
如：modules为true的情况下，每个模块都有自己的运行环境，被引入的module无法使用引入该模块的module中导入的变量，在plugins中配置webpack.ProviderPlugin可以实现插入变量的功能。<br>
举例说明：<br>
a.js引入b.js，a.js中import了jquery，b中需要使用到jquery，正常模式下必须在b中再次导入jquery，但是针对于某些第三方模块，我们无法去改变它的默认代码，这个时候可以在webpack.config.js中配置</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">plugins:[
	new webpack.ProvidePlugin({
		$: &#39;jquery&#39;
	}),
]
</code></pre></td></tr></table>
</div>
</div><p>这样，被引入的模块中的$都能在不用添加导入jquery代码而被正确使用。</p>
<h3 id="6library基本配置">6.Library基本配置</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-javascript" data-lang="javascript">
<span class="p">...</span>
<span class="nx">externals</span><span class="o">:</span><span class="s1">&#39;lodash&#39;</span><span class="p">,</span>
<span class="nx">output</span><span class="o">:</span><span class="p">{</span>
	<span class="p">...</span>
	<span class="nx">libraryTarget</span><span class="err">：</span><span class="s1">&#39;umd&#39;</span><span class="p">,</span><span class="c1">//window、this、global
</span><span class="c1"></span>	<span class="nx">library</span><span class="o">:</span><span class="s1">&#39;libraryName&#39;</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>externals代表被引入使用时定义的名字，libraryTarget中，umd代表打包生成的库可以通过各种方式引入（import、require等），配置为window、this、global则将该库的导出变量作为libraryTarget的一个变量</p>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">smartzheng</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2019-08-08
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/webpack/">webpack</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/react-hooks%E5%AD%A6%E4%B9%A0%E4%BC%98%E5%8A%BF%E5%8F%8A%E4%BD%BF%E7%94%A8/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">React Hooks学习：优势及使用</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/webpack%E5%AD%A6%E4%B9%A0%E4%B9%8Bloader/">
            <span class="next-text nav-default">webpack学习： loader</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:smartzheng1993@email.com" class="iconfont icon-email" title="email"></a>
      <a href="https://github.com/smartzheng" class="iconfont icon-github" title="github"></a>
      <a href="https://weibo.com/2062336885/profile?topnav=1&amp;wvr=6" class="iconfont icon-weibo" title="weibo"></a>
      <a href="https://www.zhihu.com/people/liu-xia-hui-89-70" class="iconfont icon-zhihu" title="zhihu"></a>
      <a href="https://www.douban.com/people/smartzheng/" class="iconfont icon-douban" title="douban"></a>
  <a href="https://smartzheng.github.io/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy; 
    2017 - 
    2020
    
  </span>
</div>
    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>
<script type="text/javascript" src="/dist/even.26188efa.min.js"></script>








</body>
</html>
